package org.imsetal.iluserservice.controller;

import jakarta.annotation.Resource;
import org.imsetal.ilapi.utils.Result;
import org.imsetal.iluserservice.pojo.User;
import org.imsetal.iluserservice.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("login")
public class LoginController {
    @Resource
    UserService userService;

    @PostMapping("")
    public Result login(@RequestBody User user) {
        System.out.println(user.getUsername());
        String info = userService.login(user.getUsername(), user.getUserpassword());
        if (info.equals("success")) {
            Map<String, String> result = new HashMap<>();
            result.put("token", userService.createUserLoginToken(user.getUsername(), user.getUserpassword()));
            return Result.success(result);
        } else {
            return Result.error("200", info);
        }
    }

    @PostMapping("checkToken")
    public Result checkToken(String token) {
        if(userService.checkUserLoginToken(token)){
            return Result.success();
        }else{
            return Result.error("201","登录信息失效");
        }
    }
}
